<!--<template>
  <div id="echart-line" :style="{ width: '100%', height: '100%' }"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  methods: {
    initChart(name, xData, yData) {
      // echarts.init('根元素','主题')
      let getchart = echarts.init(document.getElementById("echart-line"));
      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: { type: "cross" },
        },
        legend: {
          data: [name],
          bottom: "0%",
     
        },
        grid: {
          //调整图表上下左右位置
          top: "10%",
          left: "10%",
          right: "10%",
          bottom: "10%",
          containLabel: true,
        },

        xAxis: {
          type: "category",
          boundaryGap: false,
          data: xData,
          axisLabel: {
            formatter: "{value}",
            align: "center",
            // ...
          },
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: name,
            type: "line",
            data: yData,
            barWidth: "20%",
          },
        ],
      };

      getchart.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        getchart.resize();
      });
    },
  },
};
</script>-->
<template>
  <div id="echart-line" :style="{ width: '100%', height: '100%' }"></div>
</template>

<script>
import * as echarts from "echarts";

export default {
  methods: {
    initChart(name, xData, yData) {
      // 初始化图表
      let getchart = echarts.init(document.getElementById("echart-line"));

      // 配置项
      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: { type: "cross" },
          backgroundColor: "rgba(0,0,0,0.7)", // 更加美观的提示框背景色
          textStyle: {
            color: "#fff", // 提示框中文字颜色
            fontSize: 14,
          },
          borderColor: "#ccc", // 提示框边框色
          borderWidth: 1,
        },
        legend: {
          data: [name],
          bottom: "5%", // 放在图表底部
          textStyle: {
            fontSize: 14,
            color: "#666", // 更加柔和的字体颜色
          },
        },
        grid: {
          top: "15%",
          left: "10%",
          right: "10%",
          bottom: "15%",
          containLabel: true,
          backgroundColor: "rgba(0, 0, 0, 0.03)", // 添加图表背景色，略带透明
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: xData,
          axisLabel: {
            formatter: "{value}",
            align: "center",
            color: "#333", // X轴标签的颜色
            fontSize: 12,
          },
          axisLine: {
            lineStyle: {
              color: "#ccc", // X轴线条颜色
            },
          },
          axisTick: {
            show: false, // 隐藏刻度线
          },
        },
        yAxis: {
          type: "value",
          axisLabel: {
            formatter: "{value}",
            color: "#333", // Y轴标签的颜色
            fontSize: 12,
          },
          axisLine: {
            lineStyle: {
              color: "#ccc", // Y轴线条颜色
            },
          },
          axisTick: {
            show: false, // 隐藏刻度线
          },
        },
        series: [
          {
            name: name,
            type: "line",
            data: yData,
            smooth: true, // 平滑曲线
            lineStyle: {
              width: 3, // 线条宽度
              color: "#3a8ee6", // 线条颜色
              type: "solid", // 实线
            },
            itemStyle: {
              color: "#3a8ee6", // 点的颜色
              borderColor: "#fff", // 点的边框颜色
              borderWidth: 2,
            },
            areaStyle: {
              color: "rgba(58, 142, 230, 0.2)", // 渐变区域颜色
            },
            symbolSize: 8, // 数据点大小
            animationDuration: 2000, // 动画时长
          },
        ],
      };

      // 设置图表选项
      getchart.setOption(option);

      // 随着窗口大小调整图表
      window.addEventListener("resize", () => {
        getchart.resize();
      });
    },
  },
};
</script>

<style scoped>
#echart-line {
  background-color: #f5f5f5; /* 图表背景色 */
  border-radius: 10px; /* 圆角效果 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
}
</style>

